package com.flnet.web.controller.foxwash;

import com.flnet.common.annotation.Log;
import com.flnet.common.base.AjaxResult;
import com.flnet.common.enums.BusinessType;
import com.flnet.common.page.TableDataInfo;
import com.flnet.common.utils.UUIDUtils;
import com.flnet.common.utils.poi.ExcelUtil;
import com.flnet.foxwash.domain.WMachine;
import com.flnet.foxwash.domain.WServiceItem;
import com.flnet.foxwash.domain.WShop;
import com.flnet.foxwash.service.IWShopService;
import com.flnet.framework.util.ShiroUtils;
import com.flnet.framework.web.base.BaseController;
import com.flnet.system.domain.Region;
import com.flnet.system.service.IRegionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 店铺 信息操作处理
 * 
 * @author flnet
 * @date 2019-03-20
 */
@Controller
@RequestMapping("/foxwash/wShop")
public class WShopController extends BaseController
{
    private String prefix = "foxwash/wShop";
	
	@Autowired
	private IWShopService wShopService;

	@Autowired
	private IRegionService regionService;
	
	@RequiresPermissions("foxwash:wShop:view")
	@GetMapping()
	public String wShop()
	{
	    return prefix + "/wShop";
	}
	
	/**
	 * 查询店铺列表
	 */
	@RequiresPermissions("foxwash:wShop:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WShop wShop)
	{
		startPage();
        List<WShop> list = wShopService.selectWShopList(wShop);
		return getDataTable(list);
	}

	/**
	 * 导出店铺列表
	 */
	@RequiresPermissions("foxwash:wShop:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WShop wShop)
    {
    	List<WShop> list = wShopService.selectWShopList(wShop);
        ExcelUtil<WShop> util = new ExcelUtil<WShop>(WShop.class);
        return util.exportExcel(list, "wShop");
    }
	
	/**
	 * 新增店铺
	 */
	@GetMapping("/add")
	public String add(Model model)
	{
		//provinceList
		Region region = new Region();
		region.setType(1);
		List<Region> provinceList = regionService.selectRegionList(region);
		model.addAttribute("provinceList",provinceList);
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存店铺
	 */
	@RequiresPermissions("foxwash:wShop:add")
	@Log(title = "店铺", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WShop wShop)
	{
		wShop.setCreator(ShiroUtils.getUserId());
		wShop.setUuid(UUIDUtils.getUUID());
		return toAjax(wShopService.insertWShop(wShop));
	}

	/**
	 * 修改店铺
	 */
	@GetMapping("/edit/{shopId}")
	public String edit(@PathVariable("shopId") Long shopId, ModelMap mmap)
	{
		WShop wShop = wShopService.selectWShopById(shopId);
		mmap.put("wShop", wShop);

		Region region = new Region();
		region.setType(1);
		List<Region> provinceList = regionService.selectRegionList(region);
		Region province = regionService.selectRegionById(Integer.parseInt(wShop.getProvince()));

		region = new Region();
		region.setParentId(province.getId());
		Region city = regionService.selectRegionById(Integer.parseInt(wShop.getCity()));
		List<Region> cityList = regionService.selectRegionList(region);

		region = new Region();
		region.setParentId(city.getId());
		Region country = regionService.selectRegionById(Integer.parseInt(wShop.getCountry()));
		List<Region> countryList = regionService.selectRegionList(region);

		region = new Region();
		region.setParentId(country.getId());
		Region town = regionService.selectRegionById(Integer.parseInt(wShop.getTown()));
		List<Region> townList = regionService.selectRegionList(region);
		mmap.addAttribute("provinceList",provinceList);
		mmap.addAttribute("province",province);
		mmap.addAttribute("city",city);
		mmap.addAttribute("cityList",cityList);
		mmap.addAttribute("country",country);
		mmap.addAttribute("countryList",countryList);
		mmap.addAttribute("town",town);
		mmap.addAttribute("townList",townList);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存店铺
	 */
	@RequiresPermissions("foxwash:wShop:edit")
	@Log(title = "店铺", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WShop wShop)
	{		
		return toAjax(wShopService.updateWShop(wShop));
	}
	
	/**
	 * 删除店铺
	 */
	@RequiresPermissions("foxwash:wShop:remove")
	@Log(title = "店铺", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(wShopService.deleteWShopByIds(ids));
	}

	@ResponseBody
	@RequestMapping("/updateCity")
	public AjaxResult updateCity(String province){
		Region item = new Region();
		item.setParentId(Integer.parseInt(province));
		List<Region> cityList = regionService.selectRegionList(item);
		return AjaxResult.success().put("cityList",cityList);
	}

	@ResponseBody
	@RequestMapping("/updateCountry")
	public AjaxResult updateCountry(String city){
		Region item = new Region();
		item.setParentId(Integer.parseInt(city));
		List<Region> countryList = regionService.selectRegionList(item);
		return AjaxResult.success().put("countryList",countryList);
	}

	@ResponseBody
	@RequestMapping("/updateTown")
	public AjaxResult updateTown(String country){
		Region item = new Region();
		item.setParentId(Integer.parseInt(country));
		List<Region> townList = regionService.selectRegionList(item);
		return AjaxResult.success().put("townList",townList);
	}

	/**
	 * 状态修改
	 */
	@Log(title = "店铺", businessType = BusinessType.UPDATE)
	@RequiresPermissions("foxwash:wShop:edit")
	@PostMapping("/changeStatus")
	@ResponseBody
	public AjaxResult changeStatus(WShop wShop)
	{
		return toAjax(wShopService.changeStatus(wShop));
	}
}
